package com.futvan.z.erp.erp_search_engines;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.core.SuperService;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.JobUtil;
@Service
public class Erp_search_enginesService extends SuperService{
	
	/**
	 * 启动任务
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public Result erp_search_engines_list_start(HashMap<String,String> bean) throws Exception {
		Result result = new Result();
		String zids = bean.get("zids");
		if(z.isNotNull(zids)) {
			String[] zid_array = zids.split(",");
			for (String zid : zid_array) {
				//获取对象
				erp_search_engines e = selectBean("erp_search_engines_select_zid", zid);
				
				//创建参数
				Map<String,String> p = new HashMap<String,String>();
				p.put("keyinfo", e.getKeyinfo());
				p.put("zid", e.getZid());
				
				//启用任务
				JobUtil.jobAdd(zid,"信息采集|"+e.getSearch_engines(), "com.futvan.z.erp.erp_search_engines.job.job_"+e.getSearch_engines(), e.getStime(),p);
				//更新状态
				int num = update(" UPDATE erp_search_engines SET isstart = 1 WHERE zid = '"+zid+"' ");
				if(num!=1) {
					z.Exception("更新启动状态出错");
				}
			}
			result.setCode(Code.SUCCESS);
			result.setMsg("操作成功");
		}else {
			result.setCode(Code.ERROR);
			result.setMsg("请选择要操作的记录");
		}
		return result;	
	}

	/**
	 * 关闭任务
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public Result erp_search_engines_list_close(HashMap<String, String> bean) throws Exception {
		Result result = new Result();
		String zids = bean.get("zids");
		if(z.isNotNull(zids)) {
			String[] zid_array = zids.split(",");
			for (String zid : zid_array) {
				//获取对象
				erp_search_engines e = selectBean("erp_search_engines_select_zid", zid);
				//启用任务
				JobUtil.jobDelete(zid);
				//更新状态
				int num = update(" UPDATE erp_search_engines SET isstart = 0 WHERE zid = '"+zid+"' ");
				if(num!=1) {
					z.Exception("更新启动状态出错");
				}
			}
			result.setCode(Code.SUCCESS);
			result.setMsg("操作成功");
		}else {
			result.setCode(Code.ERROR);
			result.setMsg("请选择要操作的记录");
		}
		return result;	
	}

	/**
	 * 立即执行任务
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public Result erp_search_engines_list_now_start(HashMap<String, String> bean) throws Exception {
		Result result = new Result();
		String zids = bean.get("zids");
		if(z.isNotNull(zids)) {
			String[] zid_array = zids.split(",");
			for (String zid : zid_array) {
				//获取对象
				erp_search_engines e = selectBean("erp_search_engines_select_zid", zid);
				
				//创建参数
				Map<String,String> p = new HashMap<String,String>();
				p.put("keyinfo", e.getKeyinfo());
				p.put("zid", e.getZid());
				
				
				//执行任务
				JobUtil.jobRun(zid, "com.futvan.z.erp.erp_search_engines.job.job_"+e.getSearch_engines(),p);
			}
			result.setCode(Code.SUCCESS);
			result.setMsg("操作成功");
		}else {
			result.setCode(Code.ERROR);
			result.setMsg("请选择要操作的记录");
		}
		return result;	
	}
}
